import Mock, { Random } from "mockjs";

const ls = require("local-storage");

const userList = [{ username: "18012345678", password: "123456a", id: 569854 }];

// 管理员账号
const admin = { username: "admin", password: "123456a" };

const categories = [
  {
    id: +Random.string("number", 6),
    value: "notes",
    label: "音符业务",
    children: [
      { id: +Random.string("number", 6), value: "notesOnList", label: "音符上榜业务" },
      { id: +Random.string("number", 6), value: "notesCancelOnList", label: "音符不上榜业务" },
    ],
  },
  {
    id: +Random.string("number", 6),
    value: "live",
    label: "直播业务",
    children: [
      { id: +Random.string("number", 6), value: "liveInteraction", label: "直播互动弹幕" },
      { id: +Random.string("number", 6), value: "liveAll", label: "直播综合业务" },
    ],
  },
  {
    id: +Random.string("number", 6),
    value: "wdsffx",
    label: "微信业务",
    children: [{ id: +Random.string("number", 6), value: "wxVideo", label: "微信视频号业务" }],
  },
];

// 设置拦截ajax请求的相应时间
Mock.setup({
  timeout: "200-600",
});

// 管理员操作接口 ---------------开始

// 删除菜单分类
Mock.mock("/api/menu/del", "post", (option) => {
  const req = JSON.parse(option.body);
  const menuList = ls("menuList");
  if (!req.parent) {
    // 此时为子集
    let c_index;
    let p_index = menuList.findIndex((v) => {
      c_index = v.children.findIndex((j) => j.id == req.id);
      return c_index > -1;
    });
    menuList[p_index].children.splice(c_index, 1);
  } else {
    // 此时为父集
    let p_index = menuList.findIndex((v) => v.id == req.id);
    menuList.splice(p_index, 1);
  }
  ls("menuList", menuList);
  return {
    data: {
      menuList,
    },
    meta: {
      code: 0,
      success: true,
    },
  };
});

// 修改菜单分类名称
Mock.mock("/api/menu/edit", "post", (option) => {
  const req = JSON.parse(option.body);
  const menuList = ls("menuList");
  if (!req.item.parent) {
    // 此时为子集
    let c_index;
    let p_index = menuList.findIndex((v) => {
      c_index = v.children.findIndex((j) => j.id == req.item.id);
      return c_index > -1;
    });
    menuList[p_index].children[c_index].label = req.name;
  } else {
    // 此时为父集
    let p_index = menuList.findIndex((v) => v.id == req.item.id);
    menuList[p_index].label = req.name;
  }
  ls("menuList", menuList);
  return {
    data: {
      menuList,
    },
    meta: {
      code: 0,
      success: true,
    },
  };
});

// 获取菜单分类列表
Mock.mock("/api/menu/get", "post", (option) => {
  const menuList = ls("menuList") || [];
  return {
    data: {
      menuList,
    },
    meta: {
      code: 0,
      success: true,
    },
  };
});

// 添加菜单分类
Mock.mock("/api/menu/add", "post", (option) => {
  const req = JSON.parse(option.body);
  const obj = {};
  obj.id = +Random.string("number", 6);
  const menuList = ls("menuList") || [];
  if (!req.cate_p_value) {
    obj.label = req.name;
    obj.value = Random.string("lower", 6);
    obj.parent = true;
    menuList.push(obj);
  } else {
    obj.label = req.name;
    obj.value = Random.string("lower", 6);
    const index = menuList.findIndex((v) => v.value == req.cate_p_value);
    menuList[index].children = menuList[index].children ? menuList[index].children : [];
    menuList[index].children.push(obj);
  }
  ls("menuList", menuList);
  return {
    data: {
      menuList,
    },
    meta: {
      code: 0,
      success: true,
    },
  };
});

// 删除支付
Mock.mock("/api/payList/del", "post", (option) => {
  const req = JSON.parse(option.body);
  const index = ls("payList").findIndex((v) => v.id == req.id);
  const payList = ls("payList");
  payList.splice(index, 1);
  ls("payList", payList);
  return {
    meta: {
      code: 0,
      success: true,
    },
  };
});
// 根据id获取支付方式
Mock.mock("/api/payList/getByID", "post", (option) => {
  const req = JSON.parse(option.body);
  const pay = ls("payList").find((v) => v.id == req.id);
  return {
    data: {
      pay,
    },
    meta: {
      code: 0,
      success: true,
    },
  };
});
// 修改支付方式
Mock.mock("/api/payList/edit", "post", (option) => {
  const req = JSON.parse(option.body);
  const index = ls("payList").findIndex((v) => v.id == req.id);
  const payList = ls("payList");
  payList[index] = req;
  ls("payList", payList);
  return {
    meta: {
      code: 0,
      success: true,
    },
  };
});
// 添加支付方式
Mock.mock("/api/payList/save", "post", (option) => {
  const req = JSON.parse(option.body);
  req.id = +Random.string("number", 8);
  const payList = ls("payList") || [];
  payList.push(req);
  ls("payList", payList);
  return {
    meta: {
      code: 0,
      success: true,
    },
  };
});
// 获取支付列表
Mock.mock("/api/payList/get", "post", (option) => {
  const payList = ls("payList") || [];
  return {
    data: {
      payList,
    },
    meta: {
      code: 0,
      success: true,
    },
  };
});

// 删除商品分类
Mock.mock("/api/categories/del", "post", (option) => {
  const params = JSON.parse(option.body);
  const cateList = ls("cateList");
  if (!params.parent) {
    // 此时为子集
    let c_index;
    let p_index = cateList.findIndex((v) => {
      c_index = v.children.findIndex((j) => j.id == params.id);
      return c_index > -1;
    });
    cateList[p_index].children.splice(c_index, 1);
  } else {
    // 此时为父集
    let p_index = cateList.findIndex((v) => v.id == params.id);
    cateList.splice(p_index, 1);
  }
  ls("cateList", cateList);
  return {
    data: {
      cateList,
    },
    meta: {
      code: 0,
      success: true,
    },
  };
});

// 修改商品分类名称
Mock.mock("/api/categories/edit", "post", (option) => {
  const params = JSON.parse(option.body);
  const cateList = ls("cateList");
  if (!params.item.parent) {
    // 此时为子集
    let c_index;
    let p_index = cateList.findIndex((v) => {
      c_index = v.children.findIndex((j) => j.id == params.item.id);
      return c_index > -1;
    });
    cateList[p_index].children[c_index].label = params.name;
  } else {
    // 此时为父集
    let p_index = cateList.findIndex((v) => v.id == params.item.id);
    cateList[p_index].label = params.name;
  }
  ls("cateList", cateList);
  return {
    data: {
      cateList,
    },
    meta: {
      code: 0,
      success: true,
    },
  };
});

// 添加商品分类
Mock.mock("/api/categories/add", "post", (option) => {
  const params = JSON.parse(option.body);
  const obj = {};
  obj.id = +Random.string("number", 6);
  const cateList = ls("cateList") || [];
  if (!params.cate_p_value) {
    obj.label = params.name;
    obj.value = Random.string("lower", 6);
    obj.parent = true;
    cateList.push(obj);
  } else {
    obj.label = params.name;
    obj.value = Random.string("lower", 6);
    const index = cateList.findIndex((v) => v.value == params.cate_p_value);
    cateList[index].children = cateList[index].children ? cateList[index].children : [];
    cateList[index].children.push(obj);
  }
  ls("cateList", cateList);
  return {
    data: {
      cateList,
    },
    meta: {
      code: 0,
      success: true,
    },
  };
});

// 获取商品分类列表
Mock.mock("/api/categories/get", "post", (option) => {
  const cateList = ls("cateList") || [];
  return {
    data: {
      cateList,
    },
    meta: {
      code: 0,
      success: true,
    },
  };
});
// 获取商品分类列表2
Mock.mock("/api/cateTest/get", "post", (option) => {
  const cateList = ls("cateList") || [];
  return {
    data: {
      cateList,
    },
    meta: {
      code: 0,
      success: true,
    },
  };
});
// 获取商品分类列表2
Mock.mock("/api/cateTest/get", "get", (option) => {
  const cateList = ls("cateList") || [];
  return {
    data: {
      cateList,
    },
    meta: {
      code: 0,
      success: true,
    },
  };
});

// 获取用户列表
Mock.mock("/api/users/get", "post", (option) => {
  const query = JSON.parse(option.body).query;
  let users = ls("users") || [];
  if (query) {
    let newUsers = users.filter((v) => v.username.includes(query));
    users = newUsers;
  }
  return {
    data: {
      users,
    },
    meta: {
      code: 0,
      success: true,
    },
  };
});
// 添加用户
Mock.mock("/api/users/add", "post", (option) => {
  const obj = JSON.parse(option.body);
  obj.id = +Random.string("number", 6);
  obj.createTime = Random.now();
  obj.open = true;
  const users = ls("users") || [];
  users.unshift(obj);
  ls("users", users);
  return {
    data: {
      users,
    },
    meta: {
      code: 0,
      success: true,
    },
  };
});

// 修改用户信息
Mock.mock("/api/users/edit", "post", (option) => {
  const obj = JSON.parse(option.body);
  obj.editTime = Random.now();
  const users = ls("users");
  const index = users.findIndex((v) => v.id == obj.id);
  users[index] = obj;
  ls("users", users);
  return {
    data: {
      users,
    },
    meta: {
      code: 0,
      success: true,
    },
  };
});
// 修改用户状态
Mock.mock("/api/userState/edit", "post", (option) => {
  const obj = JSON.parse(option.body);
  obj.open = !obj.open;
  obj.stateChangeTime = Random.now();
  const users = ls("users");
  const index = users.findIndex((v) => v.id == obj.id);
  users[index] = obj;
  ls("users", users);
  return {
    data: {
      users,
    },
    meta: {
      code: 0,
      success: true,
    },
  };
});
// 删除用户
Mock.mock("/api/users/del", "post", (option) => {
  const obj = JSON.parse(option.body);
  const users = ls("users");
  const index = users.findIndex((v) => v.id == obj.id);
  users.splice(index, 1);
  ls("users", users);
  return {
    data: {
      users,
    },
    meta: {
      code: 0,
      success: true,
    },
  };
});
// 商品通过审核
Mock.mock("/api/product/pass", "post", (option) => {
  const obj = JSON.parse(option.body);
  obj.passTime = Random.now();
  const index = ls("productList").findIndex((v) => v.id == obj.id);
  const productList = ls("productList");
  productList[index] = obj;
  ls("productList", productList);
  return {
    meta: {
      code: 0,
      success: true,
    },
  };
});

// 商品未通过审核
Mock.mock("/api/product/noPass", "post", (option) => {
  const obj = JSON.parse(option.body);
  obj.noPassTime = Random.now();
  const index = ls("productList").findIndex((v) => v.id == obj.id);
  const productList = ls("productList");
  productList[index] = obj;
  ls("productList", productList);

  return {
    meta: {
      code: 0,
      success: true,
    },
  };
});

// 获取等待审核的商品
Mock.mock("/api/product/waitCheck/get", "post", (option) => {
  let wait = ls("productList") && ls("productList").filter((v) => v.pass == 2);
  wait = wait ? wait : [];
  ls("productWaitCheckList", wait);
  return {
    data: {
      productWaitCheckList: wait,
    },
    meta: {
      code: 0,
      success: true,
    },
  };
});

// 获取通过审核的商品2
Mock.mock("/api/product/pass/get", "get", (option) => {
  let pass = ls("productList") && ls("productList").filter((v) => v.pass == 0);
  pass = pass ? pass : [];
  ls("productPassList", pass);
  return {
    data: {
      productPassList: pass,
    },
    meta: {
      code: 0,
      success: true,
    },
  };
});
// 获取通过审核的商品
Mock.mock("/api/product/pass/get", "post", (option) => {
  let pass = ls("productList") && ls("productList").filter((v) => v.pass == 0);
  pass = pass ? pass : [];
  ls("productPassList", pass);
  return {
    data: {
      productPassList: pass,
    },
    meta: {
      code: 0,
      success: true,
    },
  };
});

// 获取未通过审核的商品
Mock.mock("/api/product/noPass/get", "post", (option) => {
  let no = ls("productList") && ls("productList").filter((v) => v.pass == 1);
  no = no ? no : [];
  ls("productNoPassList", no);
  return {
    data: {
      productNoPassList: no,
    },
    meta: {
      code: 0,
      success: true,
    },
  };
});

// 管理员操作接口 ------------------结束

// 登录
Mock.mock("/admin/api/login", "post", (option) => {
  const { username, password } = JSON.parse(option.body);
  console.log(username, password);
  if (username == admin.username && password == admin.password) {
    return {
      data: {
        admin: true,
        username,
        loginTime: Date.now(),
        expires: 60 * 60 * 1000, // 有效期为 60 分钟
        token: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9pxHIQ5i5L1kX9RX444uwnRGaIM",
      },
      meta: {
        code: 3,
        msg: "登录成功",
      },
    };
  }

  if (userList.find((v) => v.username == username && v.password === password && username != admin.username)) {
    return {
      data: {
        id: function() {
          return +Random.string("number", 6);
        },
        username,
        qq: ls("qq"),
        loginTime: Date.now(),
        expires: 60 * 60 * 1000, // 有效期为 60 分钟
        token: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9pxHIQ5i5L1kX9RX444uwnRGaIM",
      },
      meta: {
        code: 0,
        msg: "登录成功",
      },
    };
  } else if (userList.find((v) => v.username == username && v.password !== password)) {
    return {
      meta: {
        code: 2,
        msg: "账号密码错误",
      },
    };
  } else {
    return {
      meta: {
        code: 1,
        msg: "账号不存在",
      },
    };
  }
});

// 修改密码
Mock.mock("/api/updatePassword", "put", (option) => {
  const { username, oldPass, newPass } = JSON.parse(option.body).params;
  const users = ls("users");
  const newUsers = users.map((item) => {
    if (item.username == username && item.password == oldPass) {
      item.password = newPass;
      return item;
    } else {
      return item;
    }
  });
  ls.set("users", newUsers);
  // ls("users", newUsers);
  ls.set("password", newPass);
  return {
    code: 200,
    success: true,
    message: "密码修改成功",
    username: ls("username"),
    users: ls.get("users"),
  };
});

// 用户操作接口 --------------------开始
// 添加商品
Mock.mock("/api/product/save", "post", (option) => {
  const obj = JSON.parse(option.body);
  obj.id = +Random.string("number", 8);
  obj.createTime = Random.now();
  const productList = ls("productList") || [];
  productList.push(obj);
  ls("productList", productList);
  return {
    meta: {
      code: 0,
      success: true,
    },
  };
});

// 修改商品
Mock.mock("/api/product/edit", "post", (option) => {
  const obj = JSON.parse(option.body);
  obj.createTime = Random.now();
  const index = ls("productList").findIndex((v) => v.id == obj.id);
  const productList = ls("productList");
  productList[index] = obj;
  ls("productList", productList);
  return {
    meta: {
      code: 0,
      success: true,
    },
  };
});

// 删除商品
Mock.mock("/api/product/del", "post", (option) => {
  const { id } = JSON.parse(option.body);
  const index = ls("productList").findIndex((v) => v.id == id);
  const productList = ls("productList");
  productList.splice(index, 1);
  ls("productList", productList);
  return {
    meta: {
      code: 0,
      success: true,
    },
  };
});

// 获取商品
Mock.mock("/api/product/get", "post", (option) => {
  const productList = ls("productList") || [];
  return {
    data: {
      productList,
    },
    meta: {
      code: 0,
      success: true,
    },
  };
});

// 获取订单
Mock.mock("/api/orders/getAll", "post", (option) => {
  const orders = ls("orders") || [];
  console.log(ls("orders"));
  return {
    data: {
      orders,
    },
    meta: {
      code: 0,
      success: true,
    },
  };
});
// 用户操作接口 --------------------结束

// 根据id获取商品
Mock.mock("/api/product/getByID", "post", (option) => {
  const { id } = JSON.parse(option.body);
  const product = ls("productList").find((v) => v.id == id);
  return {
    data: {
      product,
    },
    meta: {
      code: 0,
      success: true,
    },
  };
});

// 获取商品详情
Mock.mock("/api/getGoodsByID", "post", (option) => {
  const { id } = JSON.parse(option.body).params;
  const item = data.items.find((v) => v.id === id);
  return {
    code: 200,
    success: true,
    item,
  };
});

// 获取账户信息
Mock.mock("/api/getAccount", "get", (option) => {
  return {
    success: true,
    message: "返回成功",
    account,
  };
});
// [{"name":"作品链接","value":"第一个输入框的值"},{"name":"备注信息","value":"第二个输入框的值"}]
// 余额明细变动接口
Mock.mock("/api/balanceRecordChange", "post", (option) => {
  const { orderWay, goodsTitle, totalPrice } = JSON.parse(option.body).params;
  const balanceRecord = ls("balanceRecord") || [];
  const obj = {};
  obj.beforeChange = account.balance;
  account.balance -= totalPrice;
  obj.money = -totalPrice;
  obj.surplus = account.balance;
  obj.detail = "";
  obj.id = +Random.string("number", 8);
  obj.orderWay = orderWay;
  obj.goodsTitle = goodsTitle;
  obj.time = Random.now();
  balanceRecord.unshift(obj);
  ls("balanceRecord", balanceRecord);
  return {
    success: true,
    message: "下单成功",
    balanceRecord,
  };
});

// 获取余额明细
Mock.mock("/api/getBalanceRecord", "get", (option) => {
  const balanceRecord = ls("balanceRecord") || [];
  return {
    success: true,
    message: "返回成功",
    balanceRecord,
  };
});

function mock(app) {
  app.post("/api/cateTest/get", (req, res) => {
    const cateList = ls("cateList") || [];
    res.json({
      data: {
        cateList,
      },
      meta: {
        code: 0,
        success: true,
      },
    });
  });
}

export default mock;
